package com.tx.admin.detailmodesidebar.controller;

import com.tx.admin.detailmodesidebar.pojo.po.DetailModeSideBar;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import com.tx.core.common.entity.ApiPageResult;
import com.tx.core.common.entity.ApiResult;
import com.tx.core.common.constant.GlobalConstant;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import com.tx.admin.detailmodesidebar.service.DetailModeSideBarService;
import com.tx.admin.detailmodesidebar.pojo.dto.DetailModeSideBarCreateDTO;
import com.tx.admin.detailmodesidebar.pojo.dto.DetailModeSideBarUpdateDTO;
import com.tx.admin.detailmodesidebar.pojo.dto.DetailModeSideBarListDTO;
import com.tx.admin.detailmodesidebar.pojo.vo.DetailModeSideBarVO;
import com.tx.core.common.entity.ImportExcelDTO;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 详情页树状结构 Controller
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-05
 */
@Tag(name =  "详情页树状结构")
@RestController
@RequestMapping("detail-mode-side-bar")
@RequiredArgsConstructor
public class DetailModeSideBarController  {

    private final DetailModeSideBarService detailModeSideBarService;

    @Operation(summary = "新增")
    @SaCheckPermission(value = "detail.mode.side.bar.create")
    @PostMapping
    public ApiResult<Void> create(@RequestBody DetailModeSideBarCreateDTO dto) {
        detailModeSideBarService.create(dto);
        return ApiResult.success();
    }

    @Operation(summary = "批量更新")
    @PostMapping("batchUpdate")
    public ApiResult<Void> batchUpdate(@RequestBody List<DetailModeSideBarCreateDTO> dto) {
        List<DetailModeSideBar> sideBars = dto.stream().map(s -> {
            DetailModeSideBar bar = new DetailModeSideBar();
            bar.setId(s.getId());
            bar.setLabel(s.getTitle());
            return bar;
        }).toList();
        detailModeSideBarService.updateBatch(sideBars);
        return ApiResult.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission(value = "detail.mode.side.bar.update")
    @PutMapping
    public ApiResult<Void> update(@RequestBody DetailModeSideBarUpdateDTO dto) {
        detailModeSideBarService.update(dto);
        return ApiResult.success();
    }

    @Operation(summary = "删除")
    @SaCheckPermission(value = "detail.mode.side.bar.remove")
    @DeleteMapping
    public ApiResult<Void> remove(@RequestBody SelectIdsDTO dto) {
        detailModeSideBarService.remove(dto);
        return ApiResult.success();
    }

    @Operation(summary = "列表查询")
    @SaCheckPermission(value = "detail.mode.side.bar.query_table")
    @GetMapping
    public ApiResult<PageResult<DetailModeSideBarVO>> list(DetailModeSideBarListDTO dto) {
        return ApiPageResult.success(detailModeSideBarService.page(dto));
    }

    @Operation(summary = "获取顺序树状集合")
    @GetMapping("/orderTrees")
    public DetailModeSideBarVO orderTrees(Long masterId) {
        return detailModeSideBarService.orderTrees(masterId);
    }

    @Operation(summary = "详情")
    @SaCheckPermission(value = "detail.mode.side.bar.query_table")
    @GetMapping("/{id}")
    public ApiResult<DetailModeSideBarVO> detail(@PathVariable Object id) {
        return ApiResult.success(detailModeSideBarService.detail(id));
    }

    @Operation(summary = "导入")
    @Parameters({
      @Parameter(name = "file", description = "上传文件", schema = @Schema(type = "string", format = "binary"), required = true),
    })
    @SaCheckPermission(value = "detail.mode.side.bar.import")
    @PostMapping("/import")
    public void importExcel(@ModelAttribute ImportExcelDTO dto) {
        detailModeSideBarService.importExcel(dto);
    }

    @Operation(summary = "导出")
    @SaCheckPermission(value = "detail.mode.side.bar.export")
    @PostMapping("/export")
    public void exportExcel(@RequestBody DetailModeSideBarListDTO dto, HttpServletResponse response) {
        detailModeSideBarService.exportExcel(dto, response);
    }
}